package com.qianfeng.zhangwei.blutooth;

import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.ListView;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.Click;
import org.androidannotations.annotations.EActivity;
import org.androidannotations.annotations.ItemClick;
import org.androidannotations.annotations.ViewById;

import java.util.ArrayList;

/**
 *
 *
 */
@EActivity(R.layout.activity_main)
public class MainActivity extends AppCompatActivity {
    private BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
    @ViewById
    Button openBtn;
    @ViewById
    Button discoverBtn;
    @ViewById
    Button searchBtn;
    BluetoothReceiver bluetoothReceiver = new BluetoothReceiver();

    @ViewById
    ListView listView;
    private DeviceAdapter deviceAdapter;
    private ArrayList<BluetoothDevice> devices = new ArrayList<>();


    @AfterViews
    public void init() {
        deviceAdapter = new DeviceAdapter(devices, this);
        listView.setAdapter(deviceAdapter);

    }

    public void open() {
        if (bluetoothAdapter != null) {
            //判断蓝牙是否打开
            if (!bluetoothAdapter.isEnabled()) {
                //隐式打开蓝牙
//                bluetoothAdapter.enable();
                //显示的打开蓝牙设备
                Intent intent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
                startActivity(intent);
            }
        }
    }

    /**
     * 服务器
     * 客服端
     * 配对
     *
     * @param postion
     */
    @ItemClick(R.id.listView)
    public void onItemClick(int postion) {

    }


    /**
     * 设置蓝牙可被发现的时间
     */
    public void show() {
        Intent intent = new Intent(BluetoothAdapter.ACTION_REQUEST_DISCOVERABLE);
        intent.putExtra(BluetoothAdapter.EXTRA_DISCOVERABLE_DURATION, 300);
        startActivity(intent);
    }

    public void search() {
        //搜索蓝牙设备
        bluetoothAdapter.startDiscovery();
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction(BluetoothDevice.ACTION_FOUND);
        registerReceiver(bluetoothReceiver, intentFilter);
    }


    @Click({R.id.openBtn, R.id.discoverBtn, R.id.searchBtn})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.openBtn:
                open();
                break;
            case R.id.discoverBtn:
                show();
                break;
            case R.id.searchBtn:
                search();
                break;

        }
    }

    public class BluetoothReceiver extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
            if (intent.getAction() == BluetoothDevice.ACTION_FOUND) {
                BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_NAME);
                devices.add(device);
                deviceAdapter.notifyDataSetChanged();
            }

        }
    }


}
